<?php
/**
 * author: crisen
 * email: crisen@crisen.org
 * date: 19-1-18
 * description:
 */

namespace App\Service;


use GuzzleHttp\Client;

class ChestOpenAll
{

    protected $url = 'http://tuixun.laiqubao.com:27777/tuixun';
    protected $client;

    protected $uuid;
    protected $addr;
    protected $door;
    protected $openid;

    public function __construct()
    {
        $this->client = new Client();
    }

    public function setChest($chestId)
    {
        $this->addr = $chestId;
    }

    public function setGrip($gripId)
    {
        $this->door = $gripId;
    }

    public function open()
    {
        // 本地测试 直接开启
        return true;
        $data = [
            'action' => 'openall',
            'uuid' => $this->uuid,
        ];
        $res = $this->send($data);
        if ($res) {
            return true;
        }
        return false;
    }


    /**
     * @param int $signal 信号强度
     * @return array|mixed|\Psr\Http\Message\ResponseInterface
     */
    public function heart($signal = 23)
    {
        $data = [
            'action' => 'open',
            'uuid' => $this->uuid,
            'rssi' => $signal
        ];
        return $this->send($data);
    }


    /**
     * @param $data
     * @return string
     */
    public function generateUrl($data): string
    {
        $query = json_encode($data);
        return $query;
    }


    /**
     * @param $data
     * @return array|mixed|\Psr\Http\Message\ResponseInterface
     */
    public function send($data)
    {
        $url = $this->generateUrl($data);
        $res = $this->client->get($url);
        $res = json_decode($res->getBody(), true);
        info($res);
        if ($res) {
            return true;
        } else {
            return false;
        }
    }


}